package com.ruoyi.buss.grid.cj.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.core.enums.ErrorCode;
import com.ruoyi.buss.active.domain.DataKhhdImport;
import com.ruoyi.buss.customer.domain.DataCustomerRecord;
import com.ruoyi.buss.customer.domain.DataWgJtcy;
import com.ruoyi.buss.grid.cj.domain.DataWgWgcj;
import com.ruoyi.buss.grid.cj.domain.DataWgWgcjHjbh;
import com.ruoyi.buss.grid.cj.domain.DataWgWgcjVillage;
import com.ruoyi.buss.grid.cj.service.IDataWgWgcjService;
import com.ruoyi.buss.grid.sq.domain.DataWgWgsqSq;
import com.ruoyi.buss.task.domain.UserTaskRelationDO;
import com.ruoyi.buss.utils.Params;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.system.api.model.LoginUser;

import io.swagger.annotations.ApiOperation;

/**
 * 网格村居Controller
 * 
 * @author sxy
 * @date 2021-07-16
 */
@RestController
//@RequestMapping("/wgcj")
@RequestMapping("/gridCj")
public class DataWgWgcjController extends BaseController
{
    @Autowired
    private IDataWgWgcjService dataWgWgcjService;

    @Autowired
	private TokenService tokenService;
    
    
    
    
    
    /**
     * 获取网格村居的镇 与 村组
	 * cjType  1村组 3 镇
     * @param vo
     * @return
     */
    @PreAuthorize(hasPermi = "buss:wgcj:selectTownOrVillage")
	@GetMapping(value = "/selectTownOrVillage")
    public AjaxResult selectTownOrVillage(DataWgWgcj vo){
    	LoginUser userInfo = tokenService.getLoginUser();
		return AjaxResult.success(dataWgWgcjService.selectTownOrVillage(vo,userInfo));
    }
    /**
     * 查询网格村居列表
     * @param vo
     * @return
     */
    @PreAuthorize(hasPermi = "buss:wgcj:queryGridCJ")
	@GetMapping(value = "/queryGridCJ")
    public TableDataInfo queryGridCJ(DataWgWgcjVillage vo){
    	String ifCheck = dataWgWgcjService.ifCheck(Params.IF_QUERY_GRIDCJ_ALL);
    	LoginUser userInfo = tokenService.getLoginUser();
		startPage();
    	List<DataWgWgcjVillage> list = dataWgWgcjService.queryGridCJList(vo,userInfo,ifCheck);
		return getDataTable(list);
    }
    
    /**
     * 	网格村居，
	 * 1总行进入支行列表
	 * 2总行和支行、客户经理进入村列表
     * @param vo
     * @return
     */
    @PreAuthorize(hasPermi = "buss:wgcj:queryGridCJAll")
   	@GetMapping(value = "/queryGridCJAll")
       public AjaxResult queryGridCJAll(DataWgWgcjVillage vo){
       	LoginUser userInfo = tokenService.getLoginUser();
       	Map<String,Object> map = new HashMap<>();
       	String ifCheck = dataWgWgcjService.ifCheck(Params.IF_QUERY_GRIDCJ_ALL);
       	DataWgWgcjVillage dataWgWgcjVillage = 	dataWgWgcjService.getGridSurvey(vo,userInfo,ifCheck);
       	
       	startPage();
       	List<DataWgWgcjVillage> list = dataWgWgcjService.queryGridCJList(vo,userInfo,ifCheck);
       	map.put("pageCommonVO",getDataTable(list));//支行商户列表
		map.put("gridSurvey", dataWgWgcjVillage); //支行概况
		map.put("qx",ifCheck);
		return AjaxResult.success(map);
    }
    
    /**
     *  获取权限
     * @return
     */
    @GetMapping(value = "/queryQx")
	public AjaxResult queryQx()
	{
		 /**
		 * 判断当前配置权限是否查询所有客户
		 */
    	String ifCheckcj = dataWgWgcjService.ifCheck(Params.IF_QUERY_GRIDCJ_ALL);
    	String ifChecksc = dataWgWgcjService.ifCheck(Params.IF_QUERY_GRIDZYSC_ALL);
    	String ifChecksq = dataWgWgcjService.ifCheck(Params.IF_QUERY_GRIDSQ_ALL);
    	String ifChecksh = dataWgWgcjService.ifCheck(Params.IF_QUERY_GRIDSH_ALL);

		Map<String,Object> map = new HashMap<String,Object>();
		map.put("cjqx",ifCheckcj);
		map.put("zyscqx",ifChecksc);
		map.put("sqqx",ifChecksq);
		map.put("shqx",ifChecksh);
		return AjaxResult.success(map);
	}
    
    
    /**
     * 网格村居进入户籍列表
     * @param vo
     * @return
     */
    @PreAuthorize(hasPermi = "buss:wgcj:gridCJVillageList")
   	@GetMapping(value = "/gridCJVillageList")
       public AjaxResult gridCJVillageList(DataWgWgcjVillage vo){
    	
		if (StringUtils.isEmpty(vo.getOrgId())) {
			return  AjaxResult.error(ErrorCode.IllegalArument);
		}
		if (StringUtils.isEmpty(vo.getVillage())) {
			return  AjaxResult.error(ErrorCode.IllegalArument);
		}
    	
       	LoginUser userInfo = tokenService.getLoginUser();
       	Map<String,Object> map = new HashMap<>();
       	String ifCheck = dataWgWgcjService.ifCheck(Params.IF_QUERY_GRIDCJ_ALL);
       	DataWgWgcjVillage dataWgWgcjVillage = dataWgWgcjService.getGridSurvey(vo,userInfo,ifCheck);

       	DataWgWgcjHjbh dataWgWgcjHjbh = new DataWgWgcjHjbh();
       	dataWgWgcjHjbh.setOrgId(vo.getOrgId());
       	dataWgWgcjHjbh.setVillage(vo.getVillage());
       	dataWgWgcjHjbh.setHjbh(vo.getHjbh());
       	dataWgWgcjHjbh.setHzxm(vo.getHzxm());
       	startPage();
       	List<DataWgWgcjHjbh> list = dataWgWgcjService.villageList(dataWgWgcjHjbh);
       	map.put("pageCommonVO" ,getDataTable(list));//支行商户列表
		map.put("gridSurvey", dataWgWgcjVillage); //支行概况
		return AjaxResult.success(map);
    }
    
    /**
     * 网格村居  进入家庭页面
     * @param vo
     * @return
     */
    @PreAuthorize(hasPermi = "buss:wgcj:gridFamily")
   	@GetMapping(value = "/gridFamily")
       public AjaxResult gridFamily(DataWgWgcj vo){
    	if (StringUtils.isEmpty(vo.getHjbh())) {
			return  AjaxResult.error(ErrorCode.IllegalArument);
		}
       	Map<String,Object> map = new HashMap<>();
       	DataWgWgcjHjbh dataWgWgcjHjbh = new DataWgWgcjHjbh();
       	dataWgWgcjHjbh.setOrgId(vo.getOrgId());
       	dataWgWgcjHjbh.setVillage(vo.getVillage());
       	dataWgWgcjHjbh.setHjbh(vo.getHjbh());
       	dataWgWgcjHjbh.setHzxm(vo.getHzxm());
       	dataWgWgcjHjbh = dataWgWgcjService.getGridFamilySurvey(dataWgWgcjHjbh);
       	startPage();
    	List<DataWgWgcj> list = dataWgWgcjService.gridFamilyList(vo);
       	map.put("pageCommonVO" ,getDataTable(list));//支行商户列表
		map.put("gridFamilySurvey", dataWgWgcjHjbh); //支行概况
		return AjaxResult.success(map);
    }
    
    /**
     * 查询网格村居列表
     */
    @PreAuthorize(hasPermi = "buss:wgcj:gridPeasant")
    @GetMapping("/gridPeasant")
    public TableDataInfo gridPeasant(DataWgWgcj dataWgWgcj)
    {	
    	LoginUser userInfo = tokenService.getLoginUser();
   		String ifCheck = dataWgWgcjService.ifCheck(Params.IF_QUERY_GRIDCJ_ALL);
        startPage();
        List<DataWgWgcj> list = dataWgWgcjService.gridPeasant(dataWgWgcj,userInfo,ifCheck);
        return getDataTable(list);
    }
    

    /**
     * 修改网格村居管户
     */
    @PreAuthorize(hasPermi = "buss:wgcj:editWgcjOrg")
    @PostMapping("/editWgcjOrg")
    public AjaxResult editWgcjOrg(@RequestBody DataWgWgcj dataWgWgcj)
    {	
    	LoginUser userInfo = tokenService.getLoginUser();
        return AjaxResult.success(dataWgWgcjService.editWgcjOrg(dataWgWgcj,userInfo));
    }
    
    /**
     * 逻辑删除 网格村居用户
     */
    @PreAuthorize(hasPermi = "buss:wgcj:delWgcj")
    @PostMapping("/delWgcj")
    public AjaxResult delWgcj(@RequestBody DataWgWgcj dataWgWgcj)
    {	
    	if (StringUtils.isEmpty(dataWgWgcj.getId())) {
			return  AjaxResult.error("ID不能为空");
		}
    	
    	LoginUser userInfo = tokenService.getLoginUser();
        return AjaxResult.success(dataWgWgcjService.delWgcj(dataWgWgcj,userInfo));
    }

    /**
     * 	网格村居 和 网格社区  家庭成员信息公用
     * 	新增家庭成员
     * 	新增的同时，再网格村居和网格社区  各初始化一条数据
     * @throws Exception
     */
    @RequestMapping(value = "/addFamilyMember", method = RequestMethod.POST)
    public AjaxResult addFamilyMember(@RequestBody DataWgJtcy vo) throws Exception
    {
        if (vo == null)
        {
            return  AjaxResult.error("参数异常");
        }
        if (StringUtils.isEmpty(vo.getHjbh()))
        {
            return  AjaxResult.error("户籍编号不能为空");
        }
        if (StringUtils.isEmpty(vo.getHzxm()))
        {
            return  AjaxResult.error("户主姓名不能为空");
        }
        if (StringUtils.isEmpty(vo.getName()))
        {
            return  AjaxResult.error("家属姓名不能为空");
        }
        if (StringUtils.isEmpty(vo.getIdCard()))
        {
            return  AjaxResult.error("家属身份证号不能为空");
        }
        if (vo.getIdCard().length() != 15 && vo.getIdCard().length() != 18)
        {
            return  AjaxResult.error("家属证件号码异常");
        }
        if (StringUtils.isEmpty(vo.getYhzgx()))
        {
            return  AjaxResult.error("与户主关系不能为空");
        }

        LoginUser userInfo = tokenService.getLoginUser();

        return AjaxResult.success(dataWgWgcjService.addFamilyMember(vo, userInfo));
    }
    
    /**
     * 修改家庭成员信息
     */
    @PreAuthorize(hasPermi = "buss:wgcj:editFamilyMember")
    @PostMapping("/editFamilyMember")
    public AjaxResult editFamilyMember(@RequestBody DataWgJtcy dataWgJtcy)
    {	

		if (StringUtils.isEmpty(dataWgJtcy.getHjbh()))
		{
			return  AjaxResult.error("户籍编号不能为空");
		}
		
		if (StringUtils.isEmpty(dataWgJtcy.getId())) {
			return  AjaxResult.error("ID不能为空");
		}
	
        return AjaxResult.success(dataWgWgcjService.editFamilyMember(dataWgJtcy));
    }
    
	/**
	 * 	分户  可输入目标家庭的户籍编号，也可以自己为户主
	 *  type 1表示分户，2表示从该户籍中删除
	 */
    @PreAuthorize(hasPermi = "buss:wgcj:delFamilyMember")
    @PostMapping("/delFamilyMember")
    public AjaxResult delFamilyMember(@RequestBody DataWgJtcy dataWgJtcy)
    {	

		if (StringUtils.isEmpty(dataWgJtcy.getHjbh()))
		{
			return  AjaxResult.error("户籍编号不能为空");
		}
		if (StringUtils.isEmpty(dataWgJtcy.getIdCard()))
		{
			return AjaxResult.error("身份证号不能为空");
		}
		if (StringUtils.isEmpty(dataWgJtcy.getId())) {
			return  AjaxResult.error("ID不能为空");
		}
		if (StringUtils.isEmpty(dataWgJtcy.getType())) {
			return  AjaxResult.error("操作类型type不能为空 type 1表示分户，2表示从该户籍中删除");
		}
	
        return AjaxResult.success(dataWgWgcjService.delFamilyMember(dataWgJtcy));
    }
    
    /**
     * 网格村居提交网格营销任务
     * @param dataWgWgcj
     * @return
     */
    @PreAuthorize(hasPermi = "buss:wgcj:cjTask")
    @PostMapping("/cjTask")
    public AjaxResult cjTask(@RequestBody DataWgWgcj dataWgWgcj)
    {
    	if (StringUtils.isEmpty(dataWgWgcj.getPlanName())) {
			return  AjaxResult.error(ErrorCode.IllegalArument);
		}
    	LoginUser userInfo = tokenService.getLoginUser();
		return dataWgWgcjService.cjTask(dataWgWgcj, userInfo);	
    }
    
    @ApiOperation(value="APP网格年概况折线图")
   // @PreAuthorize(hasPermi = "buss:gridSq:wgcjAppBiYear")
    @GetMapping("/wgcjAppBiYear")
    public TableDataInfo wgcjAppBiYear()
    {
        LoginUser userInfo = tokenService.getLoginUser();

        startPage();
        List<DataWgWgcj> list = dataWgWgcjService.wgcjAppBiYear(userInfo);
        return getDataTable(list);
    }
    
    @ApiOperation(value="APP网格季度概况折线图")
   // @PreAuthorize(hasPermi = "buss:gridSq:wgcjAppBiJd")
    @GetMapping("/wgcjAppBiJd")
    public TableDataInfo wgcjAppBiJd()
    {
        LoginUser userInfo = tokenService.getLoginUser();

        startPage();
        List<DataWgWgcj> list = dataWgWgcjService.wgcjAppBiJd(userInfo);
        return getDataTable(list);
    }
    
    @ApiOperation(value="APP网格月度概况折线图")
  //  @PreAuthorize(hasPermi = "buss:gridSq:wgcjAppBiMonth")
    @GetMapping("/wgcjAppBiMonth")
    public TableDataInfo wgcjAppBiMonth()
    {
        LoginUser userInfo = tokenService.getLoginUser();

        startPage();
        List<DataWgWgcj> list = dataWgWgcjService.wgcjAppBiMonth(userInfo);
        return getDataTable(list);
    }
    
    /**
     * 转户
     * @param dataWgWgcj
     * @return
     */
    @PreAuthorize(hasPermi = "buss:wgcj:upConvert")
    @PostMapping("/upConvert")
    public AjaxResult upConvert(@RequestBody UserTaskRelationDO dataWgWgcj)
    {
    	LoginUser userInfo = tokenService.getLoginUser();
		return dataWgWgcjService.convert(dataWgWgcj,userInfo);	
    }
    
    /**
     * 查询四大网格变更记录
     * @param dataCustomerRecord
     * @return
     */
    @PreAuthorize(hasPermi = "buss:wgcj:queryWgBg")
    @GetMapping("/queryWgBg")
    public TableDataInfo queryWgBg(DataCustomerRecord dataCustomerRecord)
    {
    	LoginUser userInfo = tokenService.getLoginUser();
        startPage();
        List<DataCustomerRecord> list = dataWgWgcjService.queryWgBg(dataCustomerRecord,userInfo);
        return getDataTable(list);
    }
    /**
     *  查询客户活动信息
     * @param dataKhhdImport
     * @return
     */
    @PreAuthorize(hasPermi = "buss:wgcj:showKhhoInfo")
    @GetMapping("/showKhhoInfo")
    public TableDataInfo showKhhoInfo(DataKhhdImport dataKhhdImport)
    {
    	LoginUser userInfo = tokenService.getLoginUser();
        startPage();
        List<DataKhhdImport> list = dataWgWgcjService.showKhhoInfo(dataKhhdImport,userInfo);
        return getDataTable(list);
    }
    
    
    /**
     * 查询网格村居列表
     */
    @PreAuthorize(hasPermi = "buss:wgcj:list")
    @GetMapping("/list")
    public TableDataInfo list(DataWgWgcj dataWgWgcj)
    {
        startPage();
        List<DataWgWgcj> list = dataWgWgcjService.selectDataWgWgcjList(dataWgWgcj);
        return getDataTable(list);
    }

    /**
     * 导出网格村居列表
     */
    @PreAuthorize(hasPermi = "buss:wgcj:export")
    @Log(title = "网格村居", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DataWgWgcj dataWgWgcj) throws IOException
    {
        List<DataWgWgcj> list = dataWgWgcjService.selectDataWgWgcjList(dataWgWgcj);
        ExcelUtil<DataWgWgcj> util = new ExcelUtil<DataWgWgcj>(DataWgWgcj.class);
        util.exportExcel(response, list, "网格村居数据");
    }

    /**
     * 获取网格村居详细信息
     */
    @PreAuthorize(hasPermi = "buss:wgcj:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(dataWgWgcjService.selectDataWgWgcjById(id));
    }

    /**
     * 新增网格村居
     */
    @PreAuthorize(hasPermi = "buss:wgcj:add")
    @Log(title = "网格村居", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DataWgWgcj dataWgWgcj)
    {
        return toAjax(dataWgWgcjService.insertDataWgWgcj(dataWgWgcj));
    }

    /**
     * 修改网格村居
     */
    @PreAuthorize(hasPermi = "buss:wgcj:edit")
    @Log(title = "网格村居", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DataWgWgcj dataWgWgcj)
    {
        return toAjax(dataWgWgcjService.updateDataWgWgcj(dataWgWgcj));
    }

    /**
     * 删除网格村居
     */
    @PreAuthorize(hasPermi = "buss:wgcj:remove")
    @Log(title = "网格村居", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(dataWgWgcjService.deleteDataWgWgcjByIds(ids));
    }
}
